package com.ycsj.goldmedalnewconcept.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.ClassThirdActivity;
import com.ycsj.goldmedalnewconcept.activity.LoginActivity;
import com.ycsj.goldmedalnewconcept.activity.NewTestThirdActivity;
import com.ycsj.goldmedalnewconcept.activity.TestThirdActivity;
import com.ycsj.goldmedalnewconcept.bean.TestSecondColumnInfo;
import com.ycsj.goldmedalnewconcept.bean.TestSecondResp;
import com.ycsj.goldmedalnewconcept.bean.WordSecondListInfo;
import com.ycsj.goldmedalnewconcept.bean.WordSecondResp;
import com.ycsj.goldmedalnewconcept.bean.WordSecondResq2;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.NetConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.view.FerrisWheelLayout;

/**
 * 首页fragment ViewPager tab
 * @author dewyze
 *课前预习
 */
public class DetectionSecondFragment extends BaseFragment {
	
	private static final String TAG = "DetectionSecondFragment";
	private Activity mActivity;
	private TextView mMsgTv;
	private String mMsgName;
	private TextView tv_title;
	private RelativeLayout progressBar;
	
	
	public void setMsgName(String msgName) {
		this.mMsgName = msgName;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mActivity = activity;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_detection_second, container, false);
		fwl = (FerrisWheelLayout)view.findViewById(R.id.fw_fwl);
		fwl.setFirstNum(SPUtils.getInt(getActivity(), "firstNum"));
		return view;
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews(view);
		SPUtils.put(getActivity(), ConfigConstant.CURRENTFRAGMENT, TAG);
		int GRADE_ID =getArguments().getInt("GRADE_ID");
		String GRADE_NAME =getArguments().getString("GRADE_NAME");
		tv_title.setText(GRADE_NAME);
		handler = new Handler();
		HttpUtils httpUtils = new HttpUtils();
    	httpUtils.send(HttpMethod.GET,NetConstant.ycsj_platform+ "?type=1&gradeid="+GRADE_ID+
    			"&account="+SPUtils.getString(getActivity(), ConfigConstant.USERNAME)+
    			"&role="+SPUtil.getString(getActivity(), "role","0"),callBack);
    	
//    	httpUtils.send(HttpMethod.GET,NetConstant.CLASSTEST+
//    			"/GradeInfo.aspx?gradeid="+GRADE_ID+"&account="
//    			+SPUtils.getString(getActivity(), ConfigConstant.USERNAME)+
//    			"&usertoken="+SPUtils.getString(getActivity(), ConfigConstant.USERTOKEN),callBack);
	
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initDisplay();
	}
	
	private void initViews(View view) {
		progressBar = (RelativeLayout) view.findViewById(R.id.progressBar);
		
		tv_title = (TextView) view.findViewById(R.id.tv_title);
		TextView tv_back = (TextView) view.findViewById(R.id.tv_back);
		tv_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				HomeFragment gf = new HomeFragment();  
	    			ft.replace(R.id.center_layout, gf);
	    			ft.addToBackStack(null);
	    			ft.commit();
			}
		});
		
		
		
	
//		int unitCount=SPUtils.getInt(getContext(), "UnitCount");
////		Toast.makeText(getContext(), ""+unitCount, 0).show();
//		fwl.setcount(unitCount);
		if(SPUtils.getInt(getActivity(), "UnitCount")>4){
			int childCount = fwl.getChildCount();
			for (int i = 0; i < childCount; i++) {
				fwl.getChildAt(i).setOnClickListener(toastTagOcl);
			}
		}else{
			int childCount = 4;
			for (int i = 0; i < childCount; i++) {
				fwl.getChildAt(i).setOnClickListener(toastTagOcl);
			}
		}
		Handler handler=new Handler();
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				if(!SPUtils.getBoolean(getActivity(), ConfigConstant.IS_LOGIN_ON)){

					fwl.setcount(4);
					progressBar.setVisibility(progressBar.GONE);
					fwl.setVisibility(View.VISIBLE);
					for (int i = 8; i >3 ; i--) {
						fwl.getChildAt(i).setVisibility(View.GONE);
						
					}
				}else{
					fwl.setcount(SPUtils.getInt(getActivity(), "UnitCount"));
					progressBar.setVisibility(progressBar.GONE);
					fwl.setVisibility(View.VISIBLE);
					for (int i = 8; i >=SPUtils.getInt(getActivity(), "UnitCount") ; i--) {
						fwl.getChildAt(i).setVisibility(View.GONE);
						
					}
				}
				
				
				
//				for (int i = 0; i <9; i++) {
//					
//					fwl.getChildAt(i).setVisibility(View.VISIBLE);
//					
//					Log.e("test", "ccccc"+i);
//				}
			}
		},2000);
		
		
		
	}
	
	private void initDisplay() {
//		mMsgTv.setText(mMsgName + "");
		
//		iv_home01.setOnTouchListener(onTouch);
		
	}

	@Override
	public String getFragmentName() {
		return TAG;
	}

	
	public OnTouchListener onTouch=new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
           
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                   
                    
                    break;
                case MotionEvent.ACTION_UP:
                   
                	Animation shakeAnim = AnimationUtils.loadAnimation(getActivity(),
    						R.anim.shake);
                	v.startAnimation(shakeAnim);
                	
                    break;
            }
            return true;
        }
        
        
    };
	private FerrisWheelLayout fwl;

    public Animation shakeAnimation(int CycleTimes) {
		Animation translateAnimation = new TranslateAnimation(0, 0, -10, 10);
		translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));
		translateAnimation.setDuration(1000);
		return translateAnimation;
	}
	
    private OnClickListener toastTagOcl = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
//			if(!SPUtils.getBoolean(getActivity(), ConfigConstant.IS_LOGIN_ON)){
//				//getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_fragment_container, new NotLoggedFragment()).commit();
//				Intent intent = new Intent(getActivity(),LoginActivity.class);
//				startActivity(intent);
//			}else{
				if(!list.isEmpty()){
					
				
				TextView tv=(TextView)v;
				final int index=Integer.parseInt((String) tv.getText().toString())-SPUtils.getInt(getActivity(), "firstNum");
				Intent intent = new Intent(getActivity(),TestThirdActivity.class);
				
				
//				Intent intent = new Intent(getActivity(),NewTestThirdActivity.class);
				
				intent.putExtra("LESSON_ID", list.get(index).LESSON_ID);
				intent.putExtra("LESSON_NAME", list.get(index).LESSON_NAME);
//				Toast.makeText(getActivity(), ""+position, 0).show();
				startActivity(intent);
			}
//			}
		}
	};

	ArrayList<TestSecondColumnInfo> list=new ArrayList<TestSecondColumnInfo>();
	private RequestCallBack<String> callBack = new RequestCallBack<String>() {
		
		

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			String json = responseInfo.result;
			// 使用Gson把json解析成对象
			// 1 得到gson对象
			
//			Log.i("test", json);
			
			Gson gson = new Gson();
//			// 2 使用gson对象的方法
			if("2".equals(gson.fromJson(json, WordSecondResq2.class).RESULTTYPE)){
				//getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_fragment_container, new NotLoggedFragment()).commit();
//				Intent intent = new Intent(getActivity(),NotLoggedActivity.class);
//				startActivity(intent);
			}else{
			TestSecondResp classTestSecondResp = gson.fromJson(json, TestSecondResp.class);
			if(null!=classTestSecondResp){
				
				list.addAll(classTestSecondResp.LIST) ;
			}
			
			if(list.size()==18){
				SPUtils.put(getActivity(), "UnitCount", 18);
			}else{
				SPUtils.put(getActivity(), "UnitCount", 4);
			}
			
			handler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					if(!SPUtils.getBoolean(getActivity(), ConfigConstant.IS_LOGIN_ON)){

						fwl.setcount(4);
						progressBar.setVisibility(progressBar.GONE);
						fwl.setVisibility(View.VISIBLE);
						for (int i = 8; i >3 ; i--) {
							
							fwl.getChildAt(i).setVisibility(View.GONE);
							
						}
						
					}else{
						fwl.setcount(SPUtils.getInt(getActivity(), "UnitCount"));
						
							Log.e("LoginActivity", "ccccc"+list.size());
						
						progressBar.setVisibility(progressBar.GONE);
						fwl.setVisibility(View.VISIBLE);
						
						for (int i = 8; i >=list.size() ; i--) {
							fwl.getChildAt(i).setVisibility(View.GONE);
							
						}
					}
					
					
					
//					for (int i = 0; i <9; i++) {
//						
//						fwl.getChildAt(i).setVisibility(View.VISIBLE);
//						
//						Log.e("test", "ccccc"+i);
//					}
				}
			},2000);
			
        
			
			}
		}
		
		@Override
		public void onFailure(HttpException error, String msg) {
			
		}
	};
private Handler handler;
}
